import axios, { AxiosRequestConfig, AxiosResponse, AxiosError, InternalAxiosRequestConfig } from 'axios'

const request = axios.create({
    baseURL: import.meta.env.VITE_BASE_URL,
    timeout: 6000,
});
  /**
   * loading控制器
   * 请求拦截器
   */
request.interceptors.request.use((config: InternalAxiosRequestConfig) => {
    if (!config.headers){
        return config
    }
    config.headers.Authorization = `Bearer ${localStorage.getItem("token")}`
    config.headers["Content-Type"] = "application/json;charset=utf-8"
    return config;
})
/**
 * 响应拦截器
 */
request.interceptors.response.use(
    (res: AxiosResponse) => {
        const { data, status, statusText } = res;
        if (status === 200) return data;
        //提示警告
        console.warn(statusText);
        return new Promise(() => {});
    },
    (err: AxiosError) => {
        const { message } = err;
        //提示错误
        console.error(message);
        return new Promise(() => {});
    }
)
/**
 * 导出方法
 */
export default async <T = unknown>(params: AxiosRequestConfig) => {
    return (await request(params)) as unknown as T;
}